package com.byinsight.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.byinsight.service.ExpenseService;
import com.byinsight.service.dto.Expense;

import eg.backend.domain.Dimension;
import eg.backend.domain.fly.AggregatedRecord;
import eg.backend.manager.ExpenseManager;
import eg.backend.query.QueryDefinition;
import eg.backend.repository.DimensionRepository;
import eg.backend.util.StringUtil;

public class ExpenseServiceImpl implements ExpenseService {
	@Autowired
	private ExpenseManager expenseManager;
	@Autowired
	private DimensionRepository dimensionRepository;
	
	public List<AggregatedRecord> findAggregated(com.byinsight.service.dto.QueryDefinition queryDefinition) {
		return expenseManager.findByQueryDefinition(fromDto(queryDefinition));
	}

	public List<Expense> findDetail(com.byinsight.service.dto.QueryDefinition queryDefinition) {
		List<eg.backend.domain.Expense> result = expenseManager.findDetailByQueryDefinition(fromDto(queryDefinition));
		List<Expense> dtos = new ArrayList<Expense>();
		for(eg.backend.domain.Expense expense : result) {
			dtos.add(toDto(expense));
		}
		return dtos;
	}

	public List<eg.backend.domain.Expense> update(List<eg.backend.domain.Expense> toUpdate, List<eg.backend.domain.Expense> toDelete) {
		return expenseManager.update(toUpdate, toDelete);
	}

	public List<Expense> findDaily(Date date) {
		List<eg.backend.domain.Expense> result = expenseManager.findDailyExpense(date);
		List<Expense> dtos = new ArrayList<Expense>();
		for(eg.backend.domain.Expense expense : result) {
			dtos.add(toDto(expense));
		}
		return dtos;
	}

	private Expense toDto(eg.backend.domain.Expense expense) {
		Expense dto = new Expense();
		dto.setAmount(expense.getAmount());
		dto.setDate(expense.getDate());
		dto.setId(expense.getId());
		dto.setName(expense.getName());
		dto.setOwner(expense.getOwner());
		dto.setRemark(expense.getRemark());
		dto.setShopName(expense.getShopName() == null ? "" : expense.getShopName());
		dto.setTags(expense.getTags());
		List<Dimension> simpleDimensions = new ArrayList<Dimension>();
		for(String dimensionId : expense.getDimensions()) {
			Dimension dim = dimensionRepository.get(dimensionId);
			simpleDimensions.add(dim);
		}
		dto.setSimpleDimensions(simpleDimensions);
		return dto;
	}
	
	private QueryDefinition fromDto(com.byinsight.service.dto.QueryDefinition queryDefinition) {
		QueryDefinition qd = new QueryDefinition();
		qd.setStartDate(queryDefinition.getStartDate());
		qd.setEndDate(queryDefinition.getEndDate());
		qd.setUser(queryDefinition.getUser());
		qd.setName(queryDefinition.getName());
		qd.setTimeDimension(queryDefinition.getTimeDimension());
		qd.setTag(queryDefinition.getTag());
		qd.setShop(queryDefinition.getShop());
		
		if(StringUtil.isEmpty(queryDefinition.getViewBy()) || "date".equals(queryDefinition.getViewBy()))
			qd.setViewBy(null);
		else
			qd.setViewBy(dimensionRepository.get(queryDefinition.getViewBy()));
		List<Dimension> dimensions = new ArrayList<Dimension>();
		for(String dimId : queryDefinition.getDimensions()) {
			System.out.println("dimId in ExpenseService: " + dimId);
			if(dimId == null || dimId.indexOf("TIME") == 0)
				continue;
			Dimension dim = dimensionRepository.get(dimId);
			dimensions.add(dim);
		}
		qd.setDimensions(dimensions);
		return qd;
	}
}
